<?php
namespace app\common\model;
use think\Model;

class RecordModel extends Model{
    protected  $name = 'record';
    protected  $pk   = 'record_id';
    
    public static function onBeforeInsert($model)
    {
        $model->set("create_ip", request()->ip());
    }
    
    public function setDurationAttr($value){
        return $value > 10000 ? time() - $value : $value;
    }
    
    public function getUserNotesAttr($value,$data){
        return UserNotesModel::where([
            'staff_id' => $data['staff_id'],
            'user_id' => $data['user_id']
            ])->value('user_notes');
    }
    public function user()
    {
        return $this->belongsTo(UserModel::class,'user_id','user_id')->bind(['open_id','user_name','face','mobile']);
    }
    
    public function staff()
    {
        return $this->belongsTo(StaffModel::class,'staff_id','staff_id')->bind(['name']);
    }
}